<?php
/**
 * Created by dh2y.
 * Blog: http://blog.csdn.net/sinat_22878395
 * Date: 2020/2/6 10:36
 * For:
 */

namespace app\admin\validate;

use app\admin\model\AuthRule as AuthRuleModel;
use think\Validate;

class AuthRule extends Validate
{
    protected $rule = [
        'pid' => 'checkPid',
        'name' => 'require|unique:auth_rule',
        'title'  =>  'require',
        'menu'  =>  'require|in:1,0',
        'state' => 'require|in:1,0'
    ];



    protected $field = [
        'name'      => '规则标识',
        'title'     => '权限名称',
        'menu'      =>  '类型',
        'state'     => '审核状态',
    ];

    protected $message = [

    ];

    protected $scene = [
        'add' => ['pid','name','title','menu','state'],
        'edit' => ['pid','name','title','menu','state']
    ];


    /**
     * 自定义验证器验证 菜单pid
     * @param $value
     * @param string $rule
     * @param string $data
     * @param string $field
     * @return array|bool|null|string|\think\Model
     */
    public function checkPid($value, $rule='', $data='',$field=''){
        if (empty($value)){
            return true;
        }

        $ret = AuthRuleModel::where('id',$value)->where('menu',1)->find();
        if($ret){
            return true;
        }else{
            return '非法操作';
        }
    }
}